package com.cf.spider.core.push;

import com.cf.spider.core.fetch.FetchService;
import com.cf.spider.core.model.JobInfo;
import com.cf.spider.core.model.JobTask;
import com.cf.spider.core.model.service.JobInfoService;
import com.cf.spider.core.model.service.JobTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * boot-awm-FetchJob
 * 推送每十分钟重试一次
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/7/11 上午 11:29
 */
@Slf4j
public class PushJob {

    @Autowired
    private FetchService fetchService;

    @Autowired
    private JobTaskService jobTaskService;

    @Autowired
    private JobInfoService jobInfoService;

    @Scheduled(initialDelay = 1000 * 60, fixedDelay = 1000 * 60 * 10)
    public void doFetch() {
        log.info("开始执行数据推送任务！【{}】", new Date());
        List<JobTask> taskList = jobTaskService.getAllPushTask();
        if (taskList != null && taskList.size() > 0) {
            log.info("抓取任务总计为：【{}】", taskList.size());
            taskList.forEach(task -> {
                JobInfo jobInfo = jobInfoService.selectOne(task.getJobId());
                if (jobInfo == null) {
                    log.info("任务【{}】为找到对应的jobInfo信息，无法获取推送地址，不执行推送任务", task);
                    return;
                }
                fetchService.push(jobInfo, task);
            });
        }
    }


}
